<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps :active="1" align-center process-status="wait" style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-form label-width="120px">
      <el-form-item label="课程标题">
        <el-input controls-position="right" placeholder=" 示例:机器学习项目课:从基础到搭建项目视" v-model="courseInfo.title"></el-input>
      </el-form-item>
      <!-- 所属分类 TODO -->
      <el-form-item label="课程分类">
        <el-select @change="subjectLevelOneChanged" placeholder="请选择" v-model="courseInfo.subjectParentId">
          <el-option :key="subject.id" :label="subject.title" :value="subject.id" v-for="subject in subjectOneList"/>
        </el-select>
        <!-- 二级分类 -->
        <el-select placeholder="请选择" v-model="courseInfo.subjectId">
          <el-option :key="subject.value" :label="subject.title" :value="subject.id" v-for="subject in subjectTwoList"/>
        </el-select>
      </el-form-item>
      <!-- 课程讲师 TODO -->

      <el-form-item label="课程讲师">
        <el-select placeholder="请选择" v-model="courseInfo.teacherId">
          <el-option :key="teacher.id" :label="teacher.name" :value="teacher.id" v-for="teacher in teacherList"/>
        </el-select>
      </el-form-item>
      <el-form-item label="总课时">
        <el-input-number
          :min="0"
          controls-position="right"
          placeholder="
数"
          v-model="courseInfo.lessonNum"
        />
      </el-form-item>
      <!-- 课程简介 TODO -->
      <el-form-item label="课程简介">
        <tinymce :height="300" v-model="courseInfo.description"/>
      </el-form-item>
      <!-- 课程封面 TODO -->
      <el-form-item label="课程封面">
        <el-upload :action="BASE_API + '/eduoss/fileoss'" :before-upload="beforeAvatarUpload"
                   :on-success="handleAvatarSuccess"
                   :show-file-list="false" class="avatar-uploader">
          <img :src="courseInfo.cover" width="400px"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="课程价格">
        <el-input-number :min="0" controls-position="right" palceholder="" v-model="courseInfo.price"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" @click="next" type="primary">保存并下一步</el-button>
      </el-form-item>
      <el-form-item></el-form-item>
    </el-form>
  </div>
</template>
<script>
  import Tinymce from '@/components/Tinymce'
  import subject from '@/api/teacher/subject'

  export default {
    components: {
      Tinymce
    },
    data() {
      return {
        saveBtnDisabled: false, // 保存按钮是否禁用
        courseInfo: {
          subjectParentId: '',
          title: '',
          subjectId: '',
          teacherId: '',
          lessonNum: 0,
          description: '',
          cover: '/static/570163.jpg',
          price: 0
        },
        courseId: '',
        BASE_API: process.env.BASE_API,
        teacherList: [],
        subjectOneList: [],
        subjectTwoList: []
      }
    },
    created() {
      if (this.$route.params && this.$route.params.id) {
        this.courseId = this.$route.params.id
        this.getCourseInfoIdFn(this.courseId)
        this.getAllSubjectFn()
      }
      console.log('info created')
      this.getAllTeacherFn()
      this.getAllSubjectFn()
    },
    methods: {
      getCourseInfoIdFn(id) {
        subject.getCourseInfoId(id).then(res => {
          this.courseInfo = res.data.courseInfoVo
          subject.getNestedTreeList().then(res => {
            this.subjectOneList = res.data.list
            for (var i = 0; i < this.subjectOneList.length; i++) {
              var oneSubject = this.subjectOneList[i]
              if (this.courseInfo.subjectParentId === oneSubject.id) {
                this.subjectTwoList = oneSubject.children
              }
            }
          })

        })
      },
      handleAvatarSuccess(res, file) {
        console.log(res) // 上传响应
        console.log(URL.createObjectURL(file.raw)) // base64编码
        this.courseInfo.cover = res.data.url
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      },
      subjectLevelOneChanged(value) {
        for (let i = 0; i < this.subjectOneList.length; i++) {
          let parentId = this.subjectOneList[i]
          if (value === parentId.id) {
            this.subjectTwoList = this.subjectOneList[i].children
            this.courseInfo.subjectId = ''
          }
        }
      },
      getAllSubjectFn() {
        subject.getNestedTreeList().then(res => {
          this.subjectOneList = res.data.list

        })
      },
      addCourseInfoFn() {
        subject
          .addCourseInfo(this.courseInfo)
          .then(res => {

            this.$router.push({path: '/edu/course/chapter/' + res.data.courseId})
          })
          .then(res => {
            return this.$message({
              type: 'success',
              message: '保存成功!'
            })
          })
      },
      updateCourseInfoFn() {
        subject.updateCourseInfo(this.courseInfo).then(res => {

          this.$router.push({path: '/edu/course/chapter/' + res.data.courseId})
        })
          .then(res => {
            return this.$message({
              type: 'success',
              message: '保存成功!'
            })
          })
      },
      next() {
        if (this.$route.params.id) {
          this.updateCourseInfoFn()
        } else {
          this.addCourseInfoFn()
        }
      },
      getAllTeacherFn() {
        subject.getAllTeacher().then(res => {
          this.teacherList = res.data.items
        })
      }
    }
  }
</script>
<style scoped>
  .tinymce-container {
    line-height: 29px;
  }
</style>
